package ec.edu.uce.tour.web.menu.componente;

import java.io.IOException;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.FacesRenderer;

import org.primefaces.model.menu.MenuElement;
import org.primefaces.model.menu.MenuItem;
import org.primefaces.model.menu.Submenu;

@FacesRenderer(componentFamily="ec.edu.uce.tour.web.menu.componente",rendererType="ec.edu.uce.tour.web.menu.componente.MenuRendered")
public class MenuRendered extends BaseMenuRendered {

	@Override
	protected void encodeMarkup(FacesContext context, AbstractMenu abstractMenu)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
        MenuSlideDown menu = (MenuSlideDown) abstractMenu; 
        writer.startElement("div", null);
        writer.startElement("ul", menu);
        
        if(menu.getElementsCount() > 0) {
        	MenuElement element = (MenuElement)menu.getElements().get(0);
        	if(element instanceof Submenu)
        	{
        		final String id = element.getId();
                writer.writeAttribute("id", id, null);
                writer.writeAttribute("class", MenuSlideDown.MENU_CLASS, null);
        	}
        	//for(int i = 0; i<menu.getElementsCount();i++){
        	encodeMenu(context, menu, menu.getElements());
        	//}
        }
        writer.endElement("ul");

        writer.endElement("div");
	}

	@Override
	protected void encodeScript(FacesContext context, AbstractMenu abstractMenu)
			throws IOException {
		// TODO Auto-generated method stub
		
	}
	
    protected void encodeMenu(FacesContext context, MenuSlideDown menu, List<MenuElement> elements) throws IOException{
		ResponseWriter writer = context.getResponseWriter();
        
        for(MenuElement element : elements) {
            if(element.isRendered()) {
                if(element instanceof Submenu) {
                	writer.startElement("li", menu);
        	        writer.startElement("a", menu);
        	        writer.writeAttribute("href", "#", null);
        	        if(((Submenu) element).getIcon()!=null){
        	        writer.startElement("img",menu);
        	        writer.writeAttribute("src", ((Submenu) element).getIcon(), null);
        	        writer.endElement("img");
        	        }
        	        writer.startElement("span", menu);
        	        writer.writeAttribute("class", AbstractMenu.MENU_ACTIVO, null);
        	        writer.endElement("span");
        	        writer.startElement("span", menu);
        	        writer.writeAttribute("class", AbstractMenu.MENU_CUBIERTA, null);
        	        writer.startElement("span", menu);
        	        if(((DefaultSubMenuSlideDown) element).getValueTitle()!=null){
        	        writer.writeAttribute("class", AbstractMenu.MENU_LINK, null);
        	        writer.writeText(((DefaultSubMenuSlideDown) element).getValueTitle(), null);
        	        }
        	        writer.endElement("span");
        	        writer.startElement("span", menu);
        	        if(((DefaultSubMenuSlideDown) element).getValueDesc()!=null){
        	        writer.writeAttribute("class", AbstractMenu.MENU_DESC, null);
        	        writer.writeText(((DefaultSubMenuSlideDown) element).getValueDesc(), null);
        	        }
        	        writer.endElement("span");
        	        writer.endElement("span");
        	        writer.endElement("a");
        	        if(((Submenu) element).getElements().size()>0)
        	        	 writer.startElement("div", menu);
        	        writer.writeAttribute("class", AbstractMenu.MENU_BOX, null);
        	        for(Object menuItem : ((Submenu) element).getElements()) {
        	               if(menuItem instanceof MenuItem)
        	                	encodeMenuItem(context, menu, (MenuItem)menuItem);
        	            
        	            }
        	        	writer.endElement("div");
        	        
        	        writer.endElement("li");

                }
               
            }
        }
    }
    
    

}
